package cn.javabb.admin.controller.sys;

import cn.javabb.common.exception.ParameterException;
import cn.javabb.common.model.ResponseModel;
import cn.javabb.core.base.BaseController;
import cn.javabb.core.constant.Constant;
import cn.javabb.core.constant.SysConstant;
import cn.javabb.core.entity.sys.UserAuthDO;
import cn.javabb.core.service.sys.UserAuthService;
import cn.javabb.core.service.sys.UserService;
import cn.javabb.log.action.DeleteAction;
import cn.javabb.log.action.UserAction;
import cn.javabb.log.annotation.EntityParam;
import cn.javabb.log.annotation.Log;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @desc:
 * @author: javabb (javabob(a)163.com)
 * @create: 2019/11/13 09:37
 */
@Data
@Controller
@RequestMapping("/admin/userAuth")
public class UserAuthAdminController extends BaseController {
    @Autowired
    private UserAuthService userAuthService;
    @Autowired
    private UserService userService;
    @GetMapping("/list")
    public String listPage(){
        return "admin/system/userAuth";
    }

    @ResponseBody
    @GetMapping("/")
    public Object list(Integer userId) {
        if (null == userId) {
            return ResponseModel.Failure("用户ID不能为空");
        }
        List<UserAuthDO> list = userAuthService.listUserAuthByUserId(userId);
        return ResponseModel.table(list);
    }
    @ResponseBody
    @PostMapping("/")
    public Object save(@EntityParam UserAuthDO userAuth) {
        if (null == userAuth) {
            throw new ParameterException();
        }
        if (userAuthService.checkExist(userAuth)) {
            return ResponseModel.Failure("一个授权类型只能绑定一个账号");
        }
        if (userAuthService.save(userAuth)) {
            return ResponseModel.Success("保存成功");
        }
        return ResponseModel.Failure("保存失败");
    }

    /**
     * 重置密码
     **/
    @RequiresPermissions("user:edit")
    @ResponseBody
    @RequestMapping("/restPwd")
    @Log(name = "重置密码",key = UserAction.EDIT_PWD,action = UserAction.class)
    public Object resetPwd(Integer userId) {
        if (null == userId) {
            return ResponseModel.Failure("重置密码失败，用户id不能为空");
        }
        if (userId.equals(SysConstant.ADMIN_ROLE_ID)) {
            return ResponseModel.Failure("无法重置超级管理员角色");
        }
        boolean result = userAuthService.resetAuthPwd(userId);
        if(result){
            return ResponseModel.Success("重置成功,新密码为："+ Constant.INIT_PWD);
        } else {
            return ResponseModel.Failure("重置失败");
        }
    }
    @RequiresPermissions("user:del")
    @ResponseBody
    @DeleteMapping("/{ids}")
    @Log(name = "用户授权删除",action = DeleteAction.class)
    public Object delete(@PathVariable String ids){
        if(StringUtils.isBlank(ids)){
            return ResponseModel.Failure("参数错误");
        }
        if (userAuthService.delete(ids)) {
            return ResponseModel.Success("删除成功");
        }
        return ResponseModel.Failure("删除失败");
    }


}
